package com.example.homemodule.adapter;

import static com.examination.mlib.constants.ClickApi.Home.click_Banner;
import static com.examination.mlib.utils.LoginThenRouterUtils.RouterBean.TYPE_BANNER;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.constants.KitArouterConstants;
import com.examination.mlib.constants.UserInfoConstants;
import com.examination.mlib.utils.LoginThenRouterUtils;
import com.examination.mlib.view.autoscrollviewpager.RecyclingPagerAdapter;
import com.example.homemodule.model.bean.HomepageEntity;
import com.example.homemodule.R;
import com.example.homemodule.utils.HomeUtils;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.GlideUtil;
import com.yilijk.base.utils.ListUtil;
import com.yilijk.base.utils.SharedPreferencesUtils;

import java.util.List;


public class BannerAutoScrollAdapter extends RecyclingPagerAdapter {

    private Context context;
    private boolean isInfiniteLoop = false;
    private List<HomepageEntity.DataBean.HomeAdBannerBean> bannerBeans;
    private LayoutInflater inflater;

    public BannerAutoScrollAdapter(Context context, List<HomepageEntity.DataBean.HomeAdBannerBean> bannerBeans) {
        this.context = context;
        this.bannerBeans = bannerBeans;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        Holder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.banner_scroll_layout, container, false);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        HomepageEntity.DataBean.HomeAdBannerBean bannerBean = bannerBeans.get(position % ListUtil.getSize(bannerBeans));

        if (bannerBean != null) {
            ClickUtils.setFastOnClickListener(holder.imageView, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClickUtils.sendEvent(click_Banner,context,"banner点击");
                    if (context == null) {
                        return;
                    }
                    String token = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.token, "");
                    String passportId = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.account_passportId, "");
                    String account_mobile = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.account_mobile, "");
                    if (token == null && account_mobile == null) {
                        HomeUtils.push(context, KitArouterConstants.LOGIN_DOCTORBUNDLE, KitArouterConstants.Login.Index);
                        LoginThenRouterUtils.RouterBean bean = new LoginThenRouterUtils.RouterBean(
                                TYPE_BANNER, "", "", bannerBean
                        );
                        LoginThenRouterUtils.getInstance().setBean(bean);
                        return;
                    }

                    if (bannerBean.isRedirect() && !TextUtils.isEmpty(bannerBean.getRedirectUrl())) {
                        //跳转活动页面
//                    ARouter.getInstance().build(ARouterConstants.HomeWebviewActivity)
//                            .withString("activityId", bannerBean.getRedirectUrl())
//                            .withBoolean("isShowLoading", true)
//                            .navigation();

//                    String url = bannerBean.getRedirectUrl();
//                    if (!url.startsWith("http") && !url.startsWith("https")) {
//                        url = DevicesUtils.getCurrentUrl() + url;
//                    } else {
//                        ARouter.getInstance().build(ARouterConstants.MineWebviewActivity)
//                                .withString("title", bannerBean.getTitle())
//                                .withString("url", url)
//                                .withBoolean("ishtml", false)
//                                .navigation();
//                    }

                        String h5Url = bannerBean.getRedirectUrl();
                        if (!TextUtils.isEmpty(h5Url) && (h5Url.startsWith("http") || h5Url.startsWith("https"))) {

                            String url = h5Url.replace("${passportId}", passportId + "");
                            url = url.replace("${token}", token + "");
                            url = url.replace("${timestamp}", System.currentTimeMillis() + "");

                            if (url.startsWith("http") || url.startsWith("https")) {
                                ARouter.getInstance().build(ARouterConstants.MineWebviewActivity)
                                        .withString("title", bannerBean.getTitle())
                                        .withBoolean("ishtml", false)
                                        .withBoolean("isNotReceivedTitle",false)
                                        .withBoolean("isNotReloadOnRestart",true)
                                        .withString("url", url)
                                        .withBoolean("isShowLoading", true).navigation();
                            }
                        }else if (h5Url !=null && h5Url.startsWith("com.patient.scheme:url?")){
//                            com.patient.scheme:url?load=https://wwwtest.yilijk.com/document/index.html?id=faceVerificationProtocol
//                        com.doctor.scheme:url?load=https%3A%2F%2Fwwwtest.yilijk.com%2Fdocument%2Findex.html%3Fid%3DfaceVerificationProtocol
//                        com.doctor.scheme:url?load=https://wwwtest.yilijk.com/document/index.html?id=faceVerificationProtocol
                            ARouter.getInstance().build(ARouterConstants.MineWebviewActivity)
                                    .withString("title", "查看详情")
                                    .withBoolean("ishtml", false)
                                    .withString("url", h5Url.split("load=")[1])
                                    .withBoolean("isShowLoading", true).navigation();
                        }else if (h5Url != null && h5Url.startsWith("com.patient.scheme://")){
                            HomeUtils.toBannerClick(h5Url);
                        }else{
                            HomeUtils.toScheme(context,h5Url);
                        }

//                        else if(h5Url.startsWith("com.patient.scheme:jumpToLivePage")){
//                            HomeUtils.toBannerClick(h5Url);
//                        }else if(h5Url.startsWith("com.patient.scheme:jumpToSalesActivity")){
//                            ARouter.getInstance().build(ARouterConstants.MineWebviewActivity)
//                                    .withString("title", "查看详情")
//                                    .withBoolean("ishtml", false)
//                                    .withString("url", h5Url.split("load=")[1])
//                                    .withBoolean("isShowLoading", true).navigation();
//                        }

                    }
                }
            });
//        Glide.with(context).load(bannerUrls.get(position % bannerUrls.size())).transform(new FitCenter()).into(holder.imageView);
            if (!TextUtils.isEmpty(bannerBean.getImageUrl())){
                GlideUtil.load(
                        context, bannerBean.getImageUrl(), holder.imageView, R.drawable.icon_default_rectangle
                );
            }
        }

        return convertView;
    }

    @Override
    public int getCount() {
        if (ListUtil.getSize(bannerBeans) > 0) {
            return isInfiniteLoop ? Integer.MAX_VALUE : ListUtil.getSize(bannerBeans);
        } else {
            return 0;
        }
    }

//    @Override
//    public int getItemPosition(@NonNull Object object) {
//        return POSITION_NONE;
//    }

    public class Holder {
        ImageView imageView;

        public Holder(View view) {
            imageView = view.findViewById(R.id.banner_img);
        }
    }

    public BannerAutoScrollAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }

}

